<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>3_回调refs中回调执行次数问题</title>
</head>
<body>
    <!-- 准备好一个“容器” -->
    <div id="test"></div>
    <!-- 引入react核心库 -->
    <script type="text/javascript" src="../js/react.development.js"></script>
    <!-- 引入react-dom 用于支持react操作DOM -->
    <script type="text/javascript" src="../js/react-dom.development.js"></script>
    <!-- 引入babel 用于将jsx转换为js -->
    <script type="text/javascript" src="../js/babel.min.js"></script>

    <script type="text/babel">  // 此处一定要写babel
    // 1. 创建组件
    class Demo extends React.Component {
        // 初始化状态
        state = {isHot: false}

        // ref
       showData = (r) => {
        console.log('r', r);
        this.input2 = r
       }

        // 点击函数
        showData2 = () => {
         const {isHot} = this.state
         this.setState({isHot: !isHot})
         console.log('input1', this.input1.value);
         console.log('input2', this.input2.value);
       }

       render(){
        return(
            <div>
                <h2>今天天气很{this.state.isHot ? '炎热' : '凉爽'}</h2>
                {/*初始化调用一次，更新时会调用2次(第1次输出的是null，但不影响使用)*/}
                <input ref={r => {this.input1 = r; console.log('@@@', r)}} type="text" /><br/>
                 {/*初始化调用一次)*/}
                 <input ref={this.showData} type="text" /><br/>
                <br/>
                <button onClick={this.showData2}>点我切换天气</button>&nbsp;
            </div>
        )
       }
    }
    // 2. 渲染虚拟组件到页面
    ReactDOM.render(<Demo a="1" b="2" />, document.getElementById('test'))

    </script>
</body>
</html>